package game.leon.war.node;

import com.google.protobuf.GeneratedMessage;
import game.leon.engine.net.GateClient;
import game.leon.engine.net.codec.GameRawMessage;
import game.leon.engine.net.codec.GateRawMessage;
import game.leon.war.defs.MessageConsts;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PreDestroy;

/**
 * User: bianpeng
 * Date: 12-1-12
 */
@Component
public class WarNode {
    private Logger logger = Logger.getLogger(getClass());
    @Value("#{app.gate_host}")
    private String host;
    @Value("#{app.gate_port}")
    private int port;
    @Autowired
    private NodeHandler nodeHandler;
    private GateClient client = new GateClient();
    @PreDestroy
    public void clean() {
        client.send(new GateRawMessage(99, MessageConsts.LOGOUT, new byte[]{}));
        logger.info("going to shutdown node");
        client.disconnect();
    }
    public void start() {
        client.connect(host, port, nodeHandler);
        // login to gate server
        client.send(new GateRawMessage(99, MessageConsts.LOGIN, new byte[]{}));
    }
    //
    // 往gate写数据
    //
    public void write(long userId, int message, GeneratedMessage vo) {
        client.send(new GateRawMessage(userId, message, vo.toByteArray()));
    }
}
